package com.springBoot.springBootSysCore.modules.repository.system;

import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.springBoot.springBootSysCore.common.repository.IBaseRepository;
import com.springBoot.springBootSysCore.modules.entity.system.SysUser;

/**
 * 用户
 * @author timo
 */
public interface SysUserRepository extends IBaseRepository<SysUser, String>{


	public SysUser findByAccount(String account);

	@Query("select count(u) from SysUser u")
	public int findAllCount();

	@Query("from SysUser where delFlag = '0'")
	public List<SysUser> findAllNotDel();
	/**
	 * 根据账户查询 没有删除 没有锁定 且激活的用户
	 * @param account
	 * @return
	 */
	@Query("from SysUser  where account = ?1 and delFlag = '0' and available='false' and activation = 'true'")
	public SysUser findByAccountAvailableAndActivation(String account);
	/**
	 * 通过账号查询 没有删除 没有锁定 的用户
	 * @author PSC
	 * @param account
	 * @return
	 */
	@Query("from SysUser  where account = ?1 and delFlag = '0' and available='false'")
	public SysUser findByAccountAvailable(String account);
	/**
	 * 通过账号查询 没有删除 且激活 的用户--无视是否锁定
	 * @author PSC
	 * @param account
	 * @return
	 */
	@Query("from SysUser  where account = ?1 and delFlag = '0'  and activation='true'")
	public SysUser findByAccountActivation(String account);
	/**
	 * 通过电话查询 没有删除 没有锁定 且激活的用户
	 * @author PSC
	 * @param phone
	 * @return
	 */
	@Query("from SysUser  where phone = ?1 and delFlag = '0' and available='false' and activation = 'true'")
	public SysUser findByPhone0(String phone);
	/**
	 * 通过电话查询 没有删除 没有锁定 的用户
	 * @author PSC
	 * @param phone
	 * @return
	 */
	@Query("from SysUser  where phone = ?1 and delFlag = '0' and available='false'")
	public SysUser findByPhone1(String phone);
	/**
	 * 通过Id集合查询用户,不管有没有删除
	 * @author PSC
	 * @param ids
	 */
	@Query("from SysUser  where id in (?1)")
	public List<SysUser> findAllById(List<String> ids);

	@Modifying
	@Query("delete from SysUser where id in (?1)")
	public void deleteByIds(List<String> asList);
	@Modifying
	@Query("update SysUser set  delFlag = '1' where id in (?1)")
	public void updateEntityByIds(List<String> asList);
	@Modifying
	@Query("update SysUser set  delFlag = '1' where id =?1")
	public void updateEntityById(String id);

	/**
	 * 根据account值或者realName值查询Id集合(like查询)
	 * @author PSC
	 * @param queryInfo
	 * @return
	 */
	@Query("select id from SysUser where account like ?1 or realName like ?1")
	public List<String> findAllIdsByAccountOrRealNamr(String queryInfo);
	/**
	 * 通过角色code和机构Id查询属于该机构下的对应角色Code的用户
	 * 如某机构下的管理员
	 * @author PSC
	 * @param roleCode 对应基础角色Code 可在AppConfig的SYSCODELI获得
	 * @param orgIds
	 * @return
	 */
	@Query("select t from SysUser t inner join t.orgList org with org.id in (?2) where t.defalutRole.code=?1" )
	public List<SysUser> findByRoleCodeAndOrgId(String roleCode,String... orgIds);

	/**
	 * 重置密码(List<Id>)
	 * @param passWord
	 * @param userIds
	 */
	@Modifying
	@Query("update SysUser set password = ?1 where id in(?2)")
	public void resetUserPassWord(String passWord,List<String> userIds);

	/**
	 * 重置密码(Id)
	 * @param passWord
	 * @param userId
	 */
	@Modifying
	@Query("update SysUser set password = ?1 where id = ?2")
	public void resetUserPassWord(String passWord,String userId);

	@Query("select account  from SysUser  where account = ?1 and delFlag = '0'")
	public List<String> findEqAccount(String account);

	@Query("select email  from SysUser  where email = ?1 and delFlag = '0'")
	public List<String> findEqEmail(String email);

	//检查重复
	@Query("select o.id from SysUser o where o.account = ?1 and delFlag = '0' and activation='true' ")
	public List<String> queryIdByAccount(String account);
	@Query("select o.id from SysUser o where o.phone = ?1 and delFlag = '0' and activation='true'")
	public List<String> queryIdByPhone(String encrypt);
	@Query("select o.id from SysUser o where o.email = ?1 and delFlag = '0' and activation='true'")
	public List<String> queryIdByEmail(String encrypt);

	@Query("select id,nickName from SysUser where userType = ?1")
    List<Object[]> findSelect(String userType);


	@Query(value="delete a.* from BASICS_USER_ATTENTION a where a.own_id = ?2 and a.att_id =?1",nativeQuery=true)
	@Modifying
    void cellAttrUser(String id, String id1);
	@Query(value="INSERT into BASICS_USER_ATTENTION(att_id,own_id) VALUES (?1,?2)",nativeQuery=true)
	@Modifying
	void attrUser(String id, String id1);

	@Query(value="select count(*) from BASICS_USER_ATTENTION a where a.own_id = ?2 and a.att_id =?1",nativeQuery=true)
	int countIsAtt(String attId, String ownId);

//	@Query("update SysUser set member = '0' where UNIX_TIMESTAMP(memberDate) < UNIX_TIMESTAMP(current_timestamp())")
	@Query(value = "update SYS_USER set member = '0' where member_date < current_timestamp()",nativeQuery = true)
	@Modifying
    void updateSysUserMember();
}
